<?php

class Application_Model_XML extends Application_Model_Files {

    protected $_xmlObject;
    protected $_filename;

    /**
     * Retourne tout les utilisateurs
     * @return simplexmlobject $xml
     */
    public function getUsers() {
        $xml = $this->loadXmlObject("User.xml");

        return $xml;
    }


    /**
     * Retourne vrai si l'utilisateur existe
     * Retourn objet utilisateur
     * @param <array> $postUser
     * @param <bool> $checkPassword
     * @return <bool|object
     */
    public function userExist($postUser, $checkPassword = false) {
        $xml = $this->loadXmlObject("User.xml");

        foreach ($xml->user as $user) {
            if ($checkPassword) {
                if ($user->email == $postUser['email'] && $user->password == $postUser['password']) {
                    $result = (object) array('nom' => $user->nom,
                                'prenom' => $user->prenom, 'role' => $user->attributes()->role, 'id' => $user->attributes()->id);
                    return $result;
                }
            } else {
                if ($user->email == $postUser['email'])
                    return true;
            }
        }
        return false;
    }

    /**
     * Mise a jour des donnees de l'utilisateur
     * @param <array> $postUser
     * @return <bool> 
     */
    public function updateUser($postUser) {

        $xml = new SimpleXMLElement(file_get_contents($this->getBaseDir() . "User.xml"));

        foreach ($xml->user as $user) {
            if ($user->email == $postUser['email']) {
                $userCols = $user->children();
                //mapping formdata et xmlnode
                foreach ($userCols as $col) {
                    $col[0] = $postUser[$col->getName()];
                }
                return $xml->asXML($this->getBaseDir() . "User.xml");
            }
        }
        return false;
    }

    /**
     * Enregistre l'utilisateur default
     * Role = user
     * @param <array> $postUser
     * @return <bool>
     */
    public function saveUser($postUser) {

        $xml = new SimpleXMLElement(file_get_contents($this->getBaseDir() . "User.xml"));

        $user = $xml->addChild('user');
        $user->addAttribute('id', 'c-'.$user->count());
        $user->addAttribute('role', 'user');

        //mapping field
        foreach ($postUser as $col => $value) {
            $user->addChild($col, $value);
        }

        $result = false;
        try {
            $result = $xml->asXML($this->getBaseDir() . "User.xml");
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }

        return $result;
    }

    /*
     * *********************************
     * *********** INVOICE *************
     * *********************************
     */
    
    public function getInvoices(){
        $xml = $this->loadXmlObject("facture.xml");
        return $xml;
    }

    public function getInvoiceByClient($idClient){

    }

    /**
     * Valide le document XML utilisant le DOMDocument
     * 
     * @param string $filename
     * @param string $schema 
     */
    public function validateDocument($filename, $schema = NULL) {
        $dom = new DOMDocument();
        $dom->load($this->getBaseDir() . $filename);

        if ($schema) {
            try {
                $dom->schemaValidate($this->getBaseDir() . $schema);
            } catch (Exception $exc) {
                Zend_Debug::dump($exc->getTraceAsString());
                Zend_Debug::dump($exc->getMessage());
            }
        } else {
            try {
                $dom->validate();
            } catch (Exception $exc) {
                Zend_Debug::dump($exc->getTraceAsString());
                Zend_Debug::dump($exc->getMessage());
            }
        }
    }

    /**
     * Instancie l'objet XML
     * @param <string> $filename
     * @return <simplexml>
     */
    public function loadXmlObject($filename=null) {

        if ($filename)
            $xml = simplexml_load_file($this->getBaseDir() . $filename);
        else {
            $xml = simplexml_load_file($this->getBaseDir() . $this->_filename);
        }
        return $xml;
    }

    /* ##################
     * GETTERS & SETTERS
     * ##################
     */

    public function setFilename($filename) {
        $this->_filename = $filename;
    }

    public function getFilename() {
        return $this->_filename;
    }

}

